/**
 * Created with IntelliJ IDEA.
 * Description:单链表
 * User: LXW
 * Date: 2021-11-30
 * Time: 17:55
 */
class Listnode {       // 节点类型
    public int val;
    public Listnode next;

    public Listnode (int data) {
        this.val = data;
    }
}
public class Linkedlist {
    public Listnode head;       // 头引用
    // 单链表打印
    public void display() {
        Listnode cur = this.head;
        while (cur != null) {
            System.out.print(cur.val+" ");
        }
        System.out.println();
    }
    // 打印指定位置的单链表
    public void print (Listnode cur) {
        while (cur != null) {
            System.out.println(cur.val+" ");
        }
        System.out.println();
    }
    // 头插法
    public void addFirst (int data) {
        Listnode node = new Listnode(data);
        node.next = head.next;
        head = node;

    }
        // 尾插法
        public void addLast(int data){
            Listnode node = new Listnode(data);
            if(this.head == null) {
                this.head = node;
            }else {
                Listnode cur = this.head;
                while (cur.next != null) {
                    cur = cur.next;
                }
                //cur.next == null;
                cur.next = node;
            }
        }
        // 在index下标插入节点data
    public void addIndex (int index, int data) {
        if (index < 0 || index > size()) {
            System.out.println("index位置不合法");
            return;
        }
        if (index == 0) {
            addFirst(data);
            return;
        }
        if (index == size()) {
            addLast(data);
            return;
        }
        Listnode node = new Listnode(data);
        Listnode cur = findIndex(index);
        node.next = cur.next;
        cur.next = node;
    }
    // 获得index下标的前一个节点的地址
    public Listnode findIndex (int index) {
        Listnode cur = this.head;
        while (index-1 != 0) {
            cur = cur.next;
            index--;
        }
        return cur;
    }
    // 获得单链表长度
    public int size() {
        Listnode cur = this.head;
        int count = 0;
        while (cur != null) {
            count++;

        }
        return count;
    }
    //删除第一次出现关键字为key的节点
    public void remove (int key) {
        if (this.head == null) {
            System.out.println("单链表为空删除失败");
            return;
        }
        if (head.val == key) {
            head = head.next;
        }
        Listnode cur = findKey(key);
        if (cur == null) {
            System.out.println("没有你要删除的节点");
            return;
        }
        Listnode del = cur.next;
        cur.next = del.next;
    }
    // 获取key节点的前一个节点的地址
    public Listnode findKey (int key) {
        Listnode cur = this.head;
        while (cur != null) {
            if (cur.next.val == key) {
                return cur;
            }
            cur = cur.next;
        }
        return null;
    }
    // 删除所有值为key的节点
    public Listnode removeAllKey (int key) {
        if (head == null) {
            System.out.println("此单链表为空表，删除失败");
            return null;
        }
        Listnode cur = this.head;
        Listnode prev = this.head;
        while (cur != null) {
            Listnode curNext = cur.next;
            if (cur.val == key) {
                prev.next = cur.next;
            }
            prev = cur;
            cur = curNext;
        }
        if (head.val == key) {
            head = head.next;
        }
        return head;

    }
}

